package eu.telecom_bretagne.xml_parser;

import org.xml.sax.*;

import android.os.Environment;
import android.util.Log;

import javax.xml.parsers.*; 
import java.io.*;
import java.util.HashMap;

public class ParserSAX {
	
	static NodeHandler osm = null;
	final static String LOG_TAG = null;
	 
		public static void main(String[] args){
			try{
				// create SAX Parser factory
				SAXParserFactory fabrique = SAXParserFactory.newInstance();

				// create SAX Parser
				SAXParser parseur = fabrique.newSAXParser();

				// read XML file through a DefaultHandler
				ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
				Log.i( LOG_TAG, "Path" + classLoader.toString());
				File fichier = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + args[0] + ".xml");
				
				osm = new NodeHandler(args[1],args[2]);
				parseur.parse(fichier, osm);


			}catch(ParserConfigurationException pce){
				Log.i( LOG_TAG, "Parser configuration error in newSAXParser()");
				Log.i( LOG_TAG, pce.getMessage());
			}catch(SAXException se){
				Log.i( LOG_TAG, "Parsing error in parse()");
				Log.i( LOG_TAG, se.getMessage());
			}catch(IOException ioe){
				Log.i( LOG_TAG, "I/O error in parse()");
				Log.i( LOG_TAG, ioe.getMessage());				
			}
		}
		
		public static HashMap<String,Object> getParseResult (){
			
			HashMap<String,Object> result = new HashMap<String, Object>();
			if(osm!=null){
				result.put("nodes", osm.getOsm_nodes());
				result.put("ways", osm.getOsm_ways());
				return result;
			}
			else 
				return null;
		}
}
